#include "window_win32.h"
#include "window.h"
#include "widget_helper_win32.h"

#include <windows.h>
#include <iostream>

using namespace krryn::gui;
using namespace krryn::system_imp;

void window_impl_win32::make(const krryn::gui::window_initializer &a_Initializer){
	DWORD l_ExtraStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
	DWORD l_Style = WS_BORDER | WS_CAPTION | WS_SYSMENU;

	RECT l_WindowRect;
	l_WindowRect.left	= (LONG)a_Initializer.m_Left;
	l_WindowRect.right	= (LONG)a_Initializer.m_Width + a_Initializer.m_Left;
	l_WindowRect.top	= (LONG)a_Initializer.m_Top;
	l_WindowRect.bottom	= (LONG)a_Initializer.m_Height + a_Initializer.m_Top;

	AdjustWindowRectEx(&l_WindowRect, l_Style, FALSE, l_ExtraStyle);

	if (!(m_hWnd = CreateWindowEx(
		l_ExtraStyle, widget_helper_win32::window_class(), 
		a_Initializer.m_Title.c_str(), 
		l_Style | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, 
		l_WindowRect.left, l_WindowRect.top,
		l_WindowRect.right - l_WindowRect.left,
		l_WindowRect.bottom - l_WindowRect.top, 
		NULL, NULL, NULL, NULL))){

		throw widget_exception("Failed to create window");
	}

	std::clog 
		<< "Window '" << a_Initializer.m_Title  
		<< "' created" << std::endl;

	UpdateWindow(m_hWnd);
	SetForegroundWindow(m_hWnd);
	SetFocus(m_hWnd);
	ShowWindow(m_hWnd, SW_SHOW);
}